harden ember-persistence — safe u32 length casts in format layer - #104
Merged
Conversation
replace bare `len() as u32` casts with checked conversions: - add format::write_len() helper that uses u32::try_from() and returns an error if the length exceeds u32::MAX - update all collection length writes in aof.rs and snapshot.rs to use the safe helper (10 call sites in AOF, 5 in snapshot) - guard encrypted ciphertext length with try_from in snapshot writer - also guard format::write_bytes() the same way while >4B items is unrealistic for in-memory collections, this prevents silent data corruption if it ever happened, and the error path is essentially free since the check always succeeds in practice.
kacy
added a commit
that referenced
this pull request
Feb 19, 2026
replace bare `len() as u32` casts with checked conversions: - add format::write_len() helper that uses u32::try_from() and returns an error if the length exceeds u32::MAX - update all collection length writes in aof.rs and snapshot.rs to use the safe helper (10 call sites in AOF, 5 in snapshot) - guard encrypted ciphertext length with try_from in snapshot writer - also guard format::write_bytes() the same way while >4B items is unrealistic for in-memory collections, this prevents silent data corruption if it ever happened, and the error path is essentially free since the check always succeeds in practice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
summary
replaces all bare
len() as u32casts in the persistence format layer with checked conversions that return errors instead of silently truncating.format::write_len()helper usingu32::try_from()format::write_bytes()the same waywhile collections exceeding u32::MAX items is unrealistic for in-memory data, this eliminates the class of silent data corruption that would occur if it ever happened. the check is essentially free in practice.
what was tested
cargo clippy -p ember-persistenceclean